#include "stdafx.h"
#include "scene/fsquad.h"

namespace natrium {
	namespace scene {
		FSQuad::FSQuad() {
			unsigned int buffers[2];

			GLfloat vtx[] = {
				-1.0f, -1.0f, 0.0f, 
				 1.0f, -1.0f, 0.0f, 
				 1.0f,  1.0f, 0.0f,
			
				-1.0f, -1.0f, 0.0f,
				 1.0f,  1.0f, 0.0f,
				-1.0f,  1.0f, 0.0f
			};

			GLfloat texCoords[] = {
				0.0f, 0.0f,
				1.0f, 0.0f,
				1.0f, 1.0f,

				0.0f, 0.0f,
				1.0f, 1.0f,
				0.0f, 1.0f
			};

			//allocate and fill the buffers
			glGenBuffers(2, buffers);

			glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
			glBufferData(GL_ARRAY_BUFFER, sizeof(vtx), vtx, GL_STATIC_DRAW);

			glBindBuffer(GL_ARRAY_BUFFER, buffers[1]);
			glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords), texCoords, GL_STATIC_DRAW);

			//allocate and fill the vertex array
			glGenVertexArrays(1, &mVAO);
		
			glBindVertexArray(mVAO);
		
			glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
			glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); //location 0 is packed per 3 floats, not normalized, no stride
			glEnableVertexAttribArray(0); //vertex attrib 0 should be filled now

			glBindBuffer(GL_ARRAY_BUFFER, buffers[1]);
			glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0); //location 2 is packed per 2 floats, not normalized per se, no stride
			glEnableVertexAttribArray(2);

			glBindVertexArray(0);
		}

		FSQuad::~FSQuad() {
			glDeleteVertexArrays(1, &mVAO);
		}

		GLuint FSQuad::getHandle() const {
			return mVAO;
		}
	}
}